package magicloan.auctionpocket.ui.activity;

import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import magicloan.auctionpocket.R;
import magicloan.auctionpocket.ui.base.BaseActivity;
import magicloan.auctionpocket.ui.fragment.FifthFragment;
import magicloan.auctionpocket.ui.fragment.FirstFragment;
import magicloan.auctionpocket.ui.fragment.FourthFragment;
import magicloan.auctionpocket.ui.fragment.SecondFragment;
import magicloan.auctionpocket.ui.fragment.ThirdFragment;

/**
 * Created by hechao on 2018/1/25.
 */
public class MainActivity extends BaseActivity {

    private ArrayList<TextView> mTvList;
    private ArrayList<ImageView> mImgList;

    private ViewPager mViewPager;
    private FirstFragment mFirstFragment;
    private SecondFragment mSecondFragment;
    private ThirdFragment mThirdFragment;
    private FourthFragment mFourthFragment;
    private FifthFragment mFifthFragment;

    private ArrayList<Fragment> arrFragmentList;

    private ImageView mLastImg ;
    private TextView mLastTv ;

    @Override
    protected int setContentView() {
        return R.layout.activity_main;
    }

    protected void setWindow() {
        if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            //全屏
//            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    @Override
    protected void beforeInitView() {
        mTvList = new ArrayList<>();
        mImgList = new ArrayList<>();
        arrFragmentList = new ArrayList<Fragment>();
        setWindow();
    }

    @Override
    protected void afterInitView() {

        mFirstFragment = new FirstFragment();
        mSecondFragment = new SecondFragment();
        mThirdFragment = new ThirdFragment();
        mFourthFragment = new FourthFragment();
        mFifthFragment = new FifthFragment();

        arrFragmentList.add(mFirstFragment);
        arrFragmentList.add(mSecondFragment);
        arrFragmentList.add(mThirdFragment);
        arrFragmentList.add(mFourthFragment);
        arrFragmentList.add(mFifthFragment);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        mViewPager.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager()));

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (mLastImg != null) {
                    mLastTv.setSelected(false);
                    mLastImg.setSelected(false);
                }
                mLastTv = mTvList.get(position);
                mLastImg = mImgList.get(position);
                mViewPager.setCurrentItem(position);
                mLastTv.setSelected(true);
                mLastImg.setSelected(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }

        });
        mViewPager.setOffscreenPageLimit(4);

        TextView tv_1 = (TextView) findViewById(R.id.tv_1);
        TextView tv_2 = (TextView) findViewById(R.id.tv_2);
        TextView tv_3 = (TextView) findViewById(R.id.tv_3);
        TextView tv_4 = (TextView) findViewById(R.id.tv_4);
        TextView tv_5 = (TextView) findViewById(R.id.tv_5);
        mTvList.add(tv_1);
        mTvList.add(tv_2);
        mTvList.add(tv_3);
        mTvList.add(tv_4);
        mTvList.add(tv_5);
        tv_1.setSelected(true);
        mLastTv = tv_1;

        ImageView img_1 = (ImageView) findViewById(R.id.img_1);
        ImageView img_2 = (ImageView) findViewById(R.id.img_2);
        ImageView img_3 = (ImageView) findViewById(R.id.img_3);
        ImageView img_4 = (ImageView) findViewById(R.id.img_4);
        ImageView img_5 = (ImageView) findViewById(R.id.img_5);
        mImgList.add(img_1);
        mImgList.add(img_2);
        mImgList.add(img_3);
        mImgList.add(img_4);
        mImgList.add(img_5);
        img_1.setSelected(true);
        mLastImg = img_1;

        findViewById(R.id.ll_1).setOnClickListener(this);
        findViewById(R.id.ll_2).setOnClickListener(this);
        findViewById(R.id.ll_3).setOnClickListener(this);
        findViewById(R.id.ll_4).setOnClickListener(this);
        findViewById(R.id.ll_5).setOnClickListener(this);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    public class MyViewPagerAdapter extends FragmentPagerAdapter {
        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int arg0) {
            return arrFragmentList.get(arg0);
        }

        @Override
        public int getCount() {
            return arrFragmentList.size();
        }

    }


    @Override
    public void onClick(View v) {

        if (mLastImg != null) {
            mLastTv.setSelected(false);
            mLastImg.setSelected(false);
        }
        switch (v.getId()) {
            case R.id.ll_1:
                mLastTv = mTvList.get(0);
                mLastImg = mImgList.get(0);
                mViewPager.setCurrentItem(0);
                break;
            case R.id.ll_2:
                mLastTv = mTvList.get(1);
                mLastImg = mImgList.get(1);
                mViewPager.setCurrentItem(1);
                break;
            case R.id.ll_3:
                mLastTv = mTvList.get(2);
                mLastImg = mImgList.get(2);
                mViewPager.setCurrentItem(2);
                break;
            case R.id.ll_4:
                mLastTv = mTvList.get(3);
                mLastImg = mImgList.get(3);
                mViewPager.setCurrentItem(3);
                break;
            case R.id.ll_5:
                mLastTv = mTvList.get(4);
                mLastImg = mImgList.get(4);
                mViewPager.setCurrentItem(4);
                break;
        }
        mLastTv.setSelected(true);
        mLastImg.setSelected(true);
    }


}
